<?php

if(!defined('IN_AACMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

$mpurl = ADMINSCRIPT.'?action=model&operation='.$operation;
$purl = 'action=model&operation='.$operation;
include_once libfile('function/modelcp');

if($operation == 'list') {

	if(submitcheck('submit')) {
		
		if(empty($_POST['ids'])) {
			cpmsg('model_choose_is_empty', $purl, 'error');
		}
	
		$ids = dimplode($_POST['ids']);
		delete_model($ids);
		
		$extra = model_menudelete($_POST['ids']);
		cpmsg('model_delete_succeed', $purl, 'succeed', $extra);

	}	
	
	$perpage = 15;
	$page = $_GET['page'] ? max(intval($_GET['page']), 1) : 1;
	$start = ($page - 1) * $perpage;
	
	$count = DB::getOne("SELECT COUNT(*) FROM ".DB::table('model'));
	$model_list = DB::getAll("SELECT * FROM ".DB::table('model')." ORDER BY mid DESC LIMIT $start,$perpage");
	
	$multipage = multi($count, $perpage, $page, $mpurl);
	
	include template('model_list', 'admin');
	
} elseif($operation == 'add') {
	
	if(submitcheck('submit')) {
		
		if(empty($_POST['mname'])) {
			cpmsg('modelname_is_empty', $purl."&mid=$mid", 'error');
		}
		
		if($mid = add_model()) {
			$extra = model_menuextra($mid);
			cpmsg('model_add_succeed', 'action=model&operation=list', 'succeed', $extra);
		} else {
			cpmsg('model_add_error', 'action=model&operation=list', 'error');
		}
		
	}
	
	include template('model_form', 'admin');
	
} elseif($operation == 'edit') {
	
	$mid = intval($_GET['mid']);
	
	if(submitcheck('submit')) {
		
		if(empty($_POST['mname'])) {
			cpmsg('modelname_is_empty', $purl."&mid=$mid", 'error');
		}
		if($_POST['msetting']) {
			$_POST['msetting'] = serialize($_POST['msetting']);
		}
		
		if(DB::update('model', $_POST, "mid='$mid'")) {
			$extra = model_menuupdate($mid);
			cpmsg('model_edit_succeed', 'action=model&operation=list', 'succeed', $extra);
		} else {
			cpmsg('model_edit_error', 'action=model&operation=list', 'error');
		}
		
	}
	
	$model = DB::getRow("SELECT * FROM ".DB::table('model')." WHERE mid='$mid'");
	if($model['msetting']) {
		$model['msetting'] = unserialize($model['msetting']);
	}
	
	include template('model_form', 'admin');
	
} elseif($operation == 'delete') {
	
	$mid = intval($_GET['mid']);
	
	delete_model($mid);
	$extra = model_menudelete($mid);
	cpmsg('model_delete_succeed', 'action=model&operation=list', 'succeed', $extra);
	
} elseif($operation == 'fieldlist') {
	
	$mid = intval($_GET['mid']);
	
	if(submitcheck('submit')) {
		
		if(empty($_POST['fields'])) {
			cpmsg('field_choose_is_empty', $purl."&mid=$mid", 'error');
		}
		
		if(delete_modelfield($mid, $_POST['fields'])) {
			cpmsg('field_delete_succeed', 'action=model&operation=fieldlist&mid='.$mid, 'succeed');
		} else {
			cpmsg('field_delete_error', 'action=model&operation=fieldlist&mid='.$mid, 'error');
		}
		
	}
	
	$mdata = DB::getOne("SELECT mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	$mdata = unserialize($mdata);
	$displayorder = array();
	
	foreach($mdata as $key => $field) {
		$mdata[$key]['type'] = htmltypelable($field['type']);
		$mdata[$key]['displayorder'] = $displayorder[] = intval($field['displayorder']);
	}
	array_multisort($displayorder, SORT_ASC, $mdata);

	include template('modelfield_list', 'admin');
	
} elseif($operation == 'fieldadd') {
	
	$mid = intval($_GET['mid']);
	
	if(submitcheck('submit')) {
		
		if(empty($_POST['name'])) {
			cpmsg('fieldname_is_empty', $purl."&mid=$mid", 'error');
		} elseif(in_array($_POST['name'], array('id', 'catid', 'dateline'))) {
			cpmsg('fieldname_is_internal', $purl."&mid=$mid", 'error');
		}
		if(($_POST['type'] == 'checkbox' || $_POST['type'] == 'radio') && !$_POST['extra']) {
			cpmsg('fieldextra_is_empty', $purl."&mid=$mid", 'error');
		}
		
		if(add_modelfield($mid)) {
			cpmsg('field_add_succeed', 'action=model&operation=fieldlist&mid='.$mid, 'succeed');
		} else {
			cpmsg('field_add_error', 'action=model&operation=fieldlist&mid='.$mid, 'error');
		}
		
	}
	
	$typeselect = htmltypeselect();
	include template('modelfield_form', 'admin');

} elseif($operation == 'fieldedit') {

	$mid = intval($_GET['mid']);
	$fieldname = trim($_GET['fieldname']);
	
	if(!$mid || !$fieldname) {
		cpmsg('parameters_error', 'action=model&operation=fieldlist&mid='.$mid, 'error');
	}
	
	if(submitcheck('submit')) {
	
		if(empty($_POST['name'])) {
			cpmsg('modelfieldname_is_empty', $purl."&mid=$mid&fieldnanme=$fieldname", 'error');
		} elseif(in_array($_POST['name'], array('id', 'catid', 'dateline'))) {
			cpmsg('fieldname_is_internal', $purl."&mid=$mid&fieldnanme=$fieldname", 'error');
		}
		if(($_POST['type'] == 'checkbox' || $_POST['type'] == 'radio') && !$_POST['extra']) {
			cpmsg('fieldextra_is_empty', $purl."&mid=$mid&fieldnanme=$fieldname", 'error');
		}
		
		if(edit_modelfield($mid, $fieldname)) {
			cpmsg('field_edit_succeed', 'action=model&operation=fieldlist&mid='.$mid, 'succeed');
		} else {
			cpmsg('field_edit_error', 'action=model&operation=fieldlist&mid='.$mid, 'error');
		}
		
	}
	
	$mdata = DB::getOne("SELECT mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	$mdata = unserialize($mdata);
	$field = $mdata[$fieldname];
	$field['extra'] = implode("\r\n", $field['extra']);
	$typeselect = htmltypeselect($field['type']);

	include template('modelfield_form', 'admin');
	
} elseif($operation == 'fielddelete') {
	
	$mid = intval($_GET['mid']);
		
	if(empty($_GET['fieldname'])) {
		cpmsg('field_choose_is_empty', $purl."&mid=$mid", 'error');
	}
		
	if(delete_modelfield($mid, array($_GET['fieldname']))) {
		cpmsg('field_delete_succeed', 'action=model&operation=fieldlist&mid='.$mid, 'succeed');
	} else {
		cpmsg('field_delete_error', 'action=model&operation=fieldlist&mid='.$mid, 'error');
	}	
} elseif($operation == 'datalist') {
	
	$mid = intval($_GET['mid']);
	$model = DB::getRow("SELECT mname, mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}
	$mname = $model['mname'];
	$mtable = $model['mtable'];
	
	if(submitcheck('submit')) {

		if(delete_data($mtable, $_POST['ids'])) {
			cpmsg('delete_secceed', 'action=model&operation=datalist&mid='.$mid, 'succeed');
		} else {
			cpmsg('delete_error', 'action=model&operation=datalist&mid='.$mid, 'error');
		}
	}
	
	$mdata = unserialize($model['mdata']);
	$displayorder = array();
	foreach($mdata as $key => $value) {
		if($value['extra'][0] || $value['status']) {
			unset($mdata[$key]);
			unset($fields[$key]);
		} else {
			$displayorder[] = intval($value['displayorder']);
		}
	}
	array_multisort($displayorder, SORT_ASC, $mdata);
	
	$fields = array_keys($mdata);
	include_once libfile('function/cache');
	loadcache('modeldatacat'.$mid);
	$modeldatacat = $_G['cache']['modeldatacat'.$mid];
	
	$perpage = 15;
	$page = $_GET['page'] ? max(intval($_GET['page']), 1) : 1;
	$start = ($page - 1) * $perpage;
	
	$count = DB::getOne("SELECT COUNT(*) FROM ".DB::table($mtable));
	$data_list = DB::getAll("SELECT * FROM ".DB::table($mtable)." ORDER BY `id` DESC LIMIT $start,$perpage");
		
	$multipage = multi($count, $perpage, $page, $mpurl);
	
	include template('data_list', 'admin');

} elseif($operation == 'dataadd') {
	
	$mid = intval($_GET['mid']);
	$model = DB::getRow("SELECT mname, mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}	
	$mname = $model['mname'];
	$mtable = $model['mtable'];
	$mdata = unserialize($model['mdata']);
	
	if(submitcheck('submit')) {
		
		$catid = intval($_POST['catid']);
		$result = checkrequired($mdata);
		if(!$result['status']) {
			cpmsg('field_quired_error', "action=model&operation=dataedit&mid=$mid&id=$id", 'error', $result['name']);
		}
		
		foreach($_POST as $key => $value) {
			if(is_array($value)) {
				$value = serialize($value);
			}
			$_POST[$key] = $value;
		}
		$_POST['dateline'] = TIMESTAMP;
		
		if($_POST['conver']) {
			$conver = unserialize(stripslashes($_POST['conver']));
			$_POST['pic'] = addslashes($conver['pic']);
			$_POST['remote'] = intval($conver['remote']);
		}
		
		if($dataid = DB::insert($mtable, $_POST)) {
			
			if($_POST['attach_ids']) {
				$newaids = array();
				$_POST['attach_ids'] = explode(',', $_POST['attach_ids']);
				foreach ($_POST['attach_ids'] as $newaid) {
					$newaid = intval($newaid);
					if($newaid) $newaids[] = $newaid;
				}
				if($newaids) {
					DB::update($mtable.'attach', array('dataid'=>$dataid), "attachid IN (".dimplode($newaids).") AND dataid='0'");
				}
			}
			
			DB::query('UPDATE '.DB::table($mtable.'cat')." SET datarows=datarows+1 WHERE catid='$catid'");
			cpmsg('add_secceed', 'action=model&operation=datalist&mid='.$mid, 'succeed');
		} else {
			cpmsg('add_error', 'action=model&operation=datalist&mid='.$mid, 'error');
		}
	}
	
	$displayorder = array();
	foreach($mdata as $key => $value) {
		$displayorder[] = intval($value['displayorder']);
	}
	array_multisort($displayorder, SORT_ASC, $mdata);
	
	include_once libfile('function/cache');
	loadcache('modeldatacat'.$mid);
	$modeldatacat = $_G['cache']['modeldatacat'.$mid];
	
	if(!$modeldatacat) {
		cpmsg('catetory_not_empty', 'action=model&operation=datacatlist&mid='.$mid, 'error');
	}
	
	$datacat_select = datacat_select('catid', false);
	$htmlformrow = make_htmlformrow($mdata);
		
	include template('data_form', 'admin');

} elseif($operation == 'dataedit') {
	
	$mid = intval($_GET['mid']);
	$id = intval($_GET['id']);
	$model = DB::getRow("SELECT mname, mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}	
	$mname = $model['mname'];
	$mtable = $model['mtable'];
	$mdata = unserialize($model['mdata']);
	
	if(submitcheck('submit')) {
		
		$result = checkrequired($mdata);
		if(!$result['status']) {
			cpmsg('field_quired_error', "action=model&operation=dataedit&mid=$mid&id=$id", 'error', $result['name']);
		}
		
		foreach($_POST as $key => $value) {
			if(is_array($value)) {
				$value = serialize($value);
			}
			$_POST[$key] = $value;
		}
		
		if($_POST['conver']) {
			$conver = unserialize(stripslashes($_POST['conver']));
			$_POST['pic'] = addslashes($conver['pic']);
			$_POST['remote'] = intval($conver['remote']);
		}
		
		
		if(DB::update($mtable, $_POST, "id='$id'")) {
			
			if($_POST['attach_ids']) {
				$newaids = array();
				$_POST['attach_ids'] = explode(',', $_POST['attach_ids']);
				foreach ($_POST['attach_ids'] as $newaid) {
					$newaid = intval($newaid);
					if($newaid) $newaids[] = $newaid;
				}
				if($newaids) {
					DB::update($mtable.'attach', array('dataid'=>$id), "attachid IN (".dimplode($newaids).") AND dataid='0'");
				}
			}
			
			cpmsg('edit_secceed', 'action=model&operation=datalist&mid='.$mid, 'succeed');
		} else {
			cpmsg('edit_error', 'action=model&operation=datalist&mid='.$mid, 'error');
		}
	}
	
	$displayorder = array();
	foreach($mdata as $key => $value) {
		$displayorder[] = intval($value['displayorder']);
	}
	array_multisort($displayorder, SORT_ASC, $mdata);
	
	
	$content = DB::getRow("SELECT * FROM ".DB::table($mtable)." WHERE id='$id'");
	$datacat_select = datacat_select('catid', false, $content['catid']);
	$htmlformrow = make_htmlformrow($mdata, $content);
	
	$page['attach_image'] = $page['attach_file'] = '';
	$query = DB::query("SELECT * FROM ".DB::table($mtable.'attach')." WHERE dataid='$id' ORDER BY attachid DESC");
	while ($value = DB::fetch($query)) {
		if($value['isimage']) {
			if($content['pic']) {
				$value['pic'] = $content['pic'];
			}
			$attach_image_body .= get_modeluploadcontent($value, 'model', $mid);
		} else {
			$attach_file_body .= get_modeluploadcontent($value, 'model', $mid);
		}
	}

	include template('data_form', 'admin');

} elseif($operation == 'datadelete') {
	
	$mid = intval($_GET['mid']);
	$id = intval($_GET['id']);
	
	$model = DB::getRow("SELECT mname, mtable FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}
	$mtable = $model['mtable'];
	
	if(delete_data($mtable, $id)) {
		cpmsg('delete_secceed', 'action=model&operation=datalist&mid='.$mid, 'succeed');
	} else {
		cpmsg('delete_error', 'action=model&operation=datalist&mid='.$mid, 'error');
	}
	
} elseif($operation == 'datacatlist') {
	
	$mid = intval($_GET['mid']);
	$model = DB::getRow("SELECT mname, mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}	
	$mname = $model['mname'];
	$mtable = $model['mtable'];
	$cattable = $mtable.'cat';
	
	include_once libfile('function/cache');
	loadcache('modeldatacat'.$mid);
	$modeldatacat = $_G['cache']['modeldatacat'.$mid];

	if(submitcheck('submit')) {
		
		foreach($_POST['name'] as $key=>$value) {
			$setarr = array();
			
			if($modeldatacat[$key] && $modeldatacat[$key]['catname'] != $value) {
				$setarr[] = "catname='$value'";
			}
			
			if($modeldatacat[$key] && $modeldatacat[$key]['displayorder'] != $_POST['neworder'][$key]) {
				$setarr[] = "displayorder='{$_POST['neworder'][$key]}'";
			}
			
			if($setarr) {
				DB::query('UPDATE '.DB::table($cattable)." SET ".implode(',',$setarr)." WHERE catid = '$key'");
			}
		}
		
		updatecache('modeldatacat');
		cpmsg('articlecat_update_succeed', 'action=model&operation=datacatlist&mid='.$mid, 'succeed');
	}
	
	if(empty($modeldatacat) && DB::getOne('SELECT COUNT(*) FROM '.DB::table($cattable))) {
		updatecache('modeldatacat');
		$modeldatacat = $_G['cache']['modeldatacat'.$mid];
	}
	
	$datacatstr = '';
	foreach ($modeldatacat as $key=>$value) {
		if($value['level'] == 0) {
			$datacatstr .= showmodeldatacatrow($key, 0, '', $mid, $cattable);
		}
	}
	
	include template('datacat_list', 'admin');

} elseif($operation == 'datacatadd') {
	
	$mid = intval($_GET['mid']);
		
	if(submitcheck('submit')) {
		
		$mtable = DB::getOne("SELECT mtable FROM ".DB::table('model')." WHERE mid='$mid'");
		
		if(DB::insert($mtable.'cat', $_POST)) {
		
			include_once libfile('function/cache');
			updatecache('modeldatacat');
			cpmsg('articlecat_add_succeed', 'action=model&operation=datacatlist&mid='.$mid, 'succeed');
		} else {
			cpmsg('articlecat_add_error', 'action=model&operation=datacatlist&mid='.$mid, 'error');
		}
		
	}

	$cate['displayorder'] = 0;	
	include template('datacat_form', 'admin');
	
} elseif($operation == 'datacatedit') {
	
	$mid = intval($_GET['mid']);
	$model = DB::getRow("SELECT mname, mtable, mdata FROM ".DB::table('model')." WHERE mid='$mid'");
	if(!$model['mname'] || !$model['mtable']) {
		cpmsg('model_not_found', 'action=model&operation=list', 'error');
	}	
	$mname = $model['mname'];
	$mtable = $model['mtable'];
	
	include_once libfile('function/cache');
	loadcache('modeldatacat'.$mid);
	$modeldatacat = $_G['cache']['modeldatacat'.$mid];

	if(submitcheck('submit')) {
		
		if($_GET['catid'] && !$modeldatacat[$_GET['catid']]) {
			cpmsg('articlecat_not_found', 'action=model&operation=datacatlist&mid='.$mid, 'error');
		} else {
			$catid = intval($_GET['catid']);
		}

		DB::update($mtable.'cat', $_POST, 'catid='.$catid);
		updatecache('modeldatacat');
		cpmsg('articlecat_edit_succeed', 'action=model&operation=datacatlist&mid='.$mid, 'succeed');
		
	} 

	if($_GET['catid'] && !$modeldatacat[$_GET['catid']]) {
		cpmsg('articlecat_not_found', 'action=model&operation=datacatlist&mid='.$mid, 'error');
	} else {
		$cate = $modeldatacat[$_GET['catid']];
	}
	
	include template('datacat_form', 'admin');

} elseif($operation == 'datacatdelete') {
	
	$mid = intval($_GET['mid']);
	$mtable = DB::getOne("SELECT mtable FROM ".DB::table('model')." WHERE mid='$mid'");
	
	include_once libfile('function/cache');
	loadcache('modeldatacat'.$mid);
	$modeldatacat = $_G['cache']['modeldatacat'.$mid];
	
	if($_GET['catid'] && !$modeldatacat[$_GET['catid']]) {
		cpmsg('datacat_not_found', 'action=model&operation=datacatlist&mid='.$mid, 'error');
	} else {
		$catids = $modeldatacat[$_GET['catid']]['children'];
		$catids[] = $_GET['catid'];

		delete_dataacat($mtable, $catids);
		cpmsg('articlecat_delete_succeed', 'action=model&operation=datacatlist&mid='.$mid, 'succeed');
	}
}




?>